// Canvas Table 核心类型定义

// 数据类型
export type CellValue = string | number | boolean | null | undefined | Date

// 数据类型枚举
export enum DataType {
  String = 'string',
  Number = 'number',
  Boolean = 'boolean',
  Date = 'date',
  JSON = 'json',
  Text = 'text'
}

// 验证结果接口
export interface ValidationResult {
  valid: boolean
  message?: string
}

// 列定义
export interface Column {
  field: string
  title: string
  width?: number
  minWidth?: number
  maxWidth?: number
  type?: DataType | string
  formatter?: (value: CellValue) => string
  validator?: (value: CellValue) => ValidationResult
  editable?: boolean
  sortable?: boolean
  filterable?: boolean
  resizable?: boolean
  visible?: boolean
  align?: 'left' | 'center' | 'right'
  headerAlign?: 'left' | 'center' | 'right'
  className?: string
  headerClassName?: string
  editType?: 'text' | 'number' | 'select' | 'textarea' | 'date' | 'datetime' | 'time' | 'checkbox' | 'color' | 'email' | 'url' | 'custom'
  editOptions?: Array<{ value: any; label: string }> | EditOptions
  customEditor?: EditorFactory
  compareFn?: (a: any, b: any) => number
}

// 编辑器配置选项
export interface EditOptions {
  // 通用选项
  placeholder?: string
  required?: boolean
  readonly?: boolean
  
  // 文本类选项
  maxLength?: number
  minLength?: number
  pattern?: string
  
  // 数字类选项
  min?: number
  max?: number
  step?: number
  precision?: number
  
  // 日期时间类选项
  dateFormat?: string
  timeFormat?: string
  minDate?: Date | string
  maxDate?: Date | string
  disabledDates?: Date[] | ((date: Date) => boolean)
  
  // 下拉选择选项
  options?: Array<{ value: any; label: string; disabled?: boolean }>
  multiple?: boolean
  searchable?: boolean
  
  // 颜色选择器选项
  colorFormat?: 'hex' | 'rgb' | 'hsl'
  showAlpha?: boolean
  presetColors?: string[]
  
  // 自定义选项
  [key: string]: any
}

// 单元格位置
export interface CellPosition {
  row: number
  col: number
}

// 单元格范围
export interface CellRange {
  start: CellPosition
  end: CellPosition
}

// 单元格修改记录
export interface CellChange {
  row: number
  col: number
  field: string
  oldValue: CellValue
  newValue: CellValue
}

// 行数据
export interface RowData {
  [field: string]: CellValue
}

// 表格选项
export interface TableOptions {
  width?: number
  height?: number
  rowHeight?: number
  headerHeight?: number
  showRowNumber?: boolean
  showGridLines?: boolean
  editable?: boolean
  multiSelect?: boolean
  columnResize?: boolean
  keyboardNavigation?: boolean
  copyPaste?: boolean
  undoRedo?: boolean
  virtualScroll?: boolean
  theme?: ThemeOptions
  
  // 分页信息（用于计算行号）
  pagination?: {
    currentPage: number
    pageSize: number
  }
  sortMode?: 'local' | 'remote'  // 新增：排序模式，local为本地排序，remote为远程排序
}

// 主题选项
export interface ThemeOptions {
  primaryColor?: string
  headerBgColor?: string
  headerTextColor?: string
  cellBgColor?: string
  cellTextColor?: string
  selectedBgColor?: string
  selectedTextColor?: string
  modifiedBgColor?: string
  borderColor?: string
  fontFamily?: string
  fontSize?: number
}

// 渲染上下文
export interface RenderContext {
  ctx: CanvasRenderingContext2D
  x: number
  y: number
  width: number
  height: number
  row: number
  col: number
  value: CellValue
  column: Column
  isSelected: boolean
  isModified: boolean
  isHeader: boolean
}

// 事件类型
export enum EventType {
  CellClick = 'cell-click',
  CellDoubleClick = 'cell-double-click',
  CellChange = 'cell-change',
  SelectionChange = 'selection-change',
  ColumnResize = 'column-resize',
  ContextMenu = 'context-menu',
  Copy = 'copy',
  Paste = 'paste',
  Cut = 'cut',
  KeyDown = 'key-down',
  EditStart = 'edit-start',
  EditEnd = 'edit-end',
  RowClick = 'row-click',
  ColumnClick = 'column-click',
  HeaderClick = 'header-click',
  BatchEdit = 'batch-edit',
  BatchDelete = 'batch-delete',
  BatchOperation = 'batch-operation',
  Sort = 'sort',
  Filter = 'filter',
  FillHandle = 'fill-handle'
}

// 选择模式
export enum SelectionMode {
  Cell = 'cell',      // 单元格选择
  Row = 'row',        // 行选择
  Column = 'column',  // 列选择
  Range = 'range'     // 范围选择
}

// 事件处理器
export type EventHandler<T = any> = (event: T) => void

// 单元格点击事件
export interface CellClickEvent {
  row: number
  col: number
  value: CellValue
  originalEvent: MouseEvent
}

// 单元格修改事件
export interface CellChangeEvent {
  row: number
  col: number
  field: string
  oldValue: CellValue
  newValue: CellValue
}

// 选择变化事件
export interface SelectionChangeEvent {
  selectedCells: CellPosition[]
  selectedRange?: CellRange | null
  selectedRows?: number[]
  selectedColumns?: number[]
  selectionMode?: SelectionMode
}

// 行点击事件
export interface RowClickEvent {
  row: number
  originalEvent: MouseEvent
}

// 列点击事件
export interface ColumnClickEvent {
  col: number
  originalEvent: MouseEvent
}

// 编辑器接口
export interface CellEditor {
  create(container: HTMLElement, value: CellValue, column: Column): void
  getValue(): CellValue
  setValue(value: CellValue): void
  destroy(): void
  focus(): void
  validate(): ValidationResult
  onValueChange?: (value: CellValue) => void
}

// 编辑器工厂
export type EditorFactory = (column: Column) => CellEditor

// 编辑器注册表
export interface EditorRegistry {
  register(type: string, factory: EditorFactory): void
  get(type: string): EditorFactory | undefined
  has(type: string): boolean
}

// 虚拟滚动视口
export interface Viewport {
  startRow: number
  endRow: number
  startCol: number
  endCol: number
  scrollTop: number
  scrollLeft: number
}

// 表格API接口
export interface TableAPI {
  // 数据操作
  setData(data: RowData[]): void
  getData(): RowData[]
  getCellValue(row: number, col: number): CellValue
  setCellValue(row: number, col: number, value: CellValue): void
  
  // 选择操作
  selectCell(row: number, col: number): void
  selectRange(range: CellRange): void
  clearSelection(): void
  getSelectedCells(): CellPosition[]
  
  // 编辑操作
  startEdit(row: number, col: number): void
  endEdit(save?: boolean): void
  isEditing(): boolean
  
  // 撤销重做
  undo(): void
  redo(): void
  canUndo(): boolean
  canRedo(): boolean
  
  // 渲染
  refresh(): void
  resize(width?: number, height?: number): void
  
  // 事件
  on(event: EventType, handler: EventHandler): void
  off(event: EventType, handler: EventHandler): void
  
  // 销毁
  destroy(): void
  isDestroyed(): boolean
}

// 批量操作事件
export interface BatchOperationEvent {
  operation: 'edit' | 'delete' | 'copy' | 'fill' | 'clear'
  cells: CellPosition[]
  value?: any
  options?: any
}

// 批量编辑配置
export interface BatchEditOptions {
  value?: any
  formula?: (oldValue: any, row: number, col: number) => any
  skipEmpty?: boolean
  skipReadonly?: boolean
} 